package cn.wolfcode.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.wolfcode.business.vo.HistoryVO;
import cn.wolfcode.business.vo.PackageAuditVO;
import cn.wolfcode.common.constant.HttpStatus;
import org.apache.poi.util.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.wolfcode.common.annotation.Log;
import cn.wolfcode.common.core.controller.BaseController;
import cn.wolfcode.common.core.domain.AjaxResult;
import cn.wolfcode.common.enums.BusinessType;
import cn.wolfcode.business.domain.CarPackageAudit;
import cn.wolfcode.business.service.ICarPackageAuditService;
import cn.wolfcode.common.utils.poi.ExcelUtil;
import cn.wolfcode.common.core.page.TableDataInfo;

/**
 * 套餐审核信息Controller
 * 
 * @author dalang
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/business/audit")
public class CarPackageAuditController extends BaseController
{
    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    /**
     * 查询套餐审核信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出套餐审核信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:audit:export')")
    @Log(title = "套餐审核信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        util.exportExcel(response, list, "套餐审核信息数据");
    }

    /**
     * 获取套餐审核信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:audit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(carPackageAuditService.selectCarPackageAuditById(id));
    }

    /**
     * 新增套餐审核信息
     */
    @PreAuthorize("@ss.hasPermi('business:audit:add')")
    @Log(title = "套餐审核信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改套餐审核信息
     */
    @PreAuthorize("@ss.hasPermi('business:audit:edit')")
    @Log(title = "套餐审核信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除套餐审核信息
     */
    @PreAuthorize("@ss.hasPermi('business:audit:remove')")
    @Log(title = "套餐审核信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }

    /**
     * 流程审核进度查看
     */
    @PreAuthorize("@ss.hasPermi('business:audit:process')")
    @Log(title = "进度查看", businessType = BusinessType.OTHER)
    @GetMapping("/process/{id}")
    public void processImg(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        InputStream inputStream = carPackageAuditService.getProcessImg(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    /**
     * 流程审核撤销
     */
    @PreAuthorize("@ss.hasPermi('business:audit:cancel')")
    @Log(title = "进度查看", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel/{id}")
    public AjaxResult auditCancel(@PathVariable("id") Long id){
        carPackageAuditService.auditCancel(id);
        return AjaxResult.success();
    }

    /**
     * 查看套餐审核历史
     */
    @PreAuthorize("@ss.hasPermi('business:audit:history')")
    @Log(title = "套餐审核", businessType = BusinessType.OTHER)
    @GetMapping("/history/{instanceId}")
    public AjaxResult history(@PathVariable Long instanceId)
    {
        List<HistoryVO> list = carPackageAuditService.queryHistory(instanceId);
        return AjaxResult.success(list);
    }

    /**
     * 查询套餐审核信息列表-todo
     */
    @PreAuthorize("@ss.hasPermi('business:audit:todo')")
    @GetMapping("/todo")
    public TableDataInfo todo(CarPackageAudit carPackageAudit)
    {
//        startPage();
        List<CarPackageAudit> list = carPackageAuditService.todoQuery(carPackageAudit);
        if (list == null || list.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo(Collections.emptyList(), 0);
//            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("未查到需要审核的数据");
            return tableDataInfo;
        }
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('business:audit:auditSubmit')")
    @Log(title = "套餐审核信息", businessType = BusinessType.UPDATE)
    @PutMapping("/auditSubmit")
    public AjaxResult auditSubmit(@Validated @RequestBody PackageAuditVO vo)
    {
        carPackageAuditService.auditSubmit(vo);
        return AjaxResult.success();
    }

    /**
     * 查询套餐已审信息列表-done
     */
    @PreAuthorize("@ss.hasPermi('business:audit:done')")
    @GetMapping("/done")
    public TableDataInfo done(CarPackageAudit carPackageAudit)
    {
//        startPage();
        List<CarPackageAudit> list = carPackageAuditService.doneQuery(carPackageAudit);
        if (list == null || list.size() == 0) {
            TableDataInfo tableDataInfo = new TableDataInfo(Collections.emptyList(), 0);
//            tableDataInfo.setCode(HttpStatus.SUCCESS);
            tableDataInfo.setMsg("您没有审核数据");
            return tableDataInfo;
        }
        return getDataTable(list);
    }
}
